home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / zoo tutorial / module 4- playing sounds / source / textpresenter.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  6.2 KB  |  184 lines

  1. import java.awt.Dimension;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.FileReader;
  7. import java.util.Vector;
  8.  
  9. import quicktime.qd.QDDrawer;
  10. import quicktime.qd.QDGraphics;
  11. import quicktime.qd.QDRect;
  12. import quicktime.QTException; 
  13.  
  14. import quicktime.app.image.ImagePresenter; 
  15.  
  16. import quicktime.qd.QDFont;
  17. import quicktime.qd.QDColor; 
  18. import quicktime.qd.QDConstants;
  19. import quicktime.std.image.GraphicsMode;
  20.  
  21.  
  22. /**
  23.  * QTZoo Module 3 - Using QuickTime to draw text
  24.  * A graphics utility class that images a block of text loaded from disk 
  25.  * and draws it in an image presenter
  26.  *
  27.  * @author Michael Hopkins
  28.  * @author Levi Brown 
  29.  * @author Apple Computer, Inc.
  30.  * @version 2.0 01/25/2000
  31.  *
  32.  * Copyright:     © Copyright 1999 Apple Computer, Inc. All rights reserved.
  33.  *    
  34.  * Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  35.  *                ("Apple") in consideration of your agreement to the following terms, and your
  36.  *                use, installation, modification or redistribution of this Apple software
  37.  *                constitutes acceptance of these terms.  If you do not agree with these terms,
  38.  *                please do not use, install, modify or redistribute this Apple software.
  39.  *
  40.  *                In consideration of your agreement to abide by the following terms, and subject
  41.  *                to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  42.  *                copyrights in this original Apple software (the "Apple Software"), to use,
  43.  *                reproduce, modify and redistribute the Apple Software, with or without
  44.  *                modifications, in source and/or binary forms; provided that if you redistribute
  45.  *                the Apple Software in its entirety and without modifications, you must retain
  46.  *                this notice and the following text and disclaimers in all such redistributions of
  47.  *                the Apple Software.  Neither the name, trademarks, service marks or logos of
  48.  *                Apple Computer, Inc. may be used to endorse or promote products derived from the
  49.  *                Apple Software without specific prior written permission from Apple.  Except as
  50.  *                expressly stated in this notice, no other rights or licenses, express or implied,
  51.  *                are granted by Apple herein, including but not limited to any patent rights that
  52.  *                may be infringed by your derivative works or by other works in which the Apple
  53.  *                Software may be incorporated.
  54.  *
  55.  *                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  56.  *                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  57.  *                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  58.  *                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  59.  *                COMBINATION WITH YOUR PRODUCTS.
  60.  *
  61.  *                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  62.  *                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  63.  *                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  64.  *                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  65.  *                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  66.  *                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  67.  *                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  68.  * 
  69.  */
  70. public class TextPresenter
  71. {
  72.     /**
  73.      *  Constructor for the TextPresenter class
  74.      *  @param filePath relative path of file where text is located
  75.      *  @param size size of area where text is to be clipped to
  76.      */
  77.     public TextPresenter( String filePath, Dimension size )  throws FileNotFoundException
  78.     {
  79.         this( TextPresenter.readTextFile(filePath), size );
  80.     }
  81.     
  82.     /**
  83.      *  Constructor for the TextPresenter class
  84.      *  @param filePath relative path of file where text is located
  85.      *  @param size size of area where text is to be clipped to
  86.      */
  87.     public TextPresenter(String[] text, Dimension size)
  88.     {    
  89.         final QDRect area = new QDRect( size.width, size.height );    // NOTE: QDRect( Dimension d ) is deprecated        
  90.         theText = text;
  91.         
  92.         try
  93.         {
  94.             gw = new QDGraphics( area );
  95.             
  96.             gw.beginDraw( new QDDrawer()
  97.                             {
  98.                                 public void draw( QDGraphics gw ) throws QTException
  99.                                 {
  100.                                     gw.textSize( 12 );
  101.                                     gw.textFont( QDFont.getFNum("Times") ); 
  102.                                     gw.textFace( QDConstants.normal );
  103.                                     gw.setForeColor( QDColor.black );
  104.                                       
  105.                                      gw.setBackColor( QDColor.white );
  106.                                     gw.eraseRect( area );
  107.                                     
  108.                                     
  109.                                     String aLine = theText[0];
  110.                                     
  111.                                     if ( gw.textWidth( aLine, 0, aLine.length() ) > area.getWidth() )
  112.                                         gw.textSize( 10 );
  113.                                     
  114.                                       
  115.                                     for ( int i = 0; i < theText.length; ++i )
  116.                                      {
  117.                                          if ( i != 0 )
  118.                                              aLine = theText[ i ];
  119.                                           gw.moveTo( 0, (i+1) * 12 );
  120.                                          gw.drawTextScaled( aLine.length(), aLine, 1.0f, 1.0f );
  121.                                        }
  122.                                   }
  123.                             }); 
  124.     
  125.             imagePres = ImagePresenter.fromGWorld( gw );
  126.             imagePres.setGraphicsMode( new GraphicsMode( QDConstants.transparent, QDColor.white ));
  127.         }
  128.         catch ( QTException e )
  129.         {
  130.             e.printStackTrace();
  131.         }
  132.     }
  133.     
  134.     /**
  135.      * Reads in a text file to a String array.
  136.      * @param filePath the path and name of the text file to read.
  137.      * @return the string array containing the contents of the file,
  138.      * one line per element of the array.
  139.      * @throws java.io.FileNotFoundException if the given file can not
  140.      * be found to be read.
  141.      */
  142.     public static String[] readTextFile( String filePath ) throws FileNotFoundException
  143.     {
  144.         if (filePath != null)
  145.         {
  146.             try
  147.             {
  148.                 BufferedReader reader = new BufferedReader( new FileReader( filePath ));
  149.                 Vector lines = new Vector();
  150.                 
  151.                 String aLine = reader.readLine();
  152.                 while( aLine != null )
  153.                 {
  154.                     lines.addElement(aLine);
  155.                     aLine = reader.readLine();
  156.                 }
  157.                 
  158.                 String[] lineArray = new String[ lines.size() ];
  159.                 lines.copyInto(lineArray);
  160.                 return lineArray;
  161.             }
  162.             catch (IOException exc)
  163.             {
  164.                 exc.printStackTrace();
  165.             }
  166.         }
  167.         
  168.         return new String[0];
  169.     }
  170.  
  171.     /**
  172.      * Returns the image presenter object that contains the rendered text
  173.      */
  174.     public ImagePresenter getPresenter()
  175.     {
  176.         return imagePres;
  177.     }
  178.     
  179.     protected QDGraphics      gw;
  180.     protected String[]          theText;
  181.     protected ImagePresenter imagePres;
  182. }
  183.  
  184.